package eu.jpereira.trainings.designpatterns.structural.decorator.channel.decorator;

import eu.jpereira.trainings.designpatterns.structural.decorator.channel.SocialChannel;

public class WordCensor extends SocialChannelDecorator {

        private String censoredWord;
       
        /**
         * @param censoredWord
         */
        public WordCensor(String censoredWord) {
               
                this.censoredWord = censoredWord;
        }
       
        /**
         * @param censoredWord
         * @param decoratedChannel
         */
        public WordCensor(String censoredWord, SocialChannel decoratedChannel) {
               
                this.censoredWord = censoredWord;
                this.delegate = decoratedChannel;
        }
       
        /*
         * (non-Javadoc)
         *
         * @see eu.jpereira.trainings.designpatterns.structural.decorator.channel.
         * SocialChannel#deliverMessage(java.lang.String)
         */
        @Override
        public void deliverMessage(String message) {

                int wordLength = censoredWord.length();
               
                for (int i = -1; (i = message.indexOf(censoredWord, i + 1)) != -1; ) {
                    for (int j = i; j < i + wordLength; j++) {
                        message = message.substring(0, j) + '*' + message.substring(j + 1);
                    }
                }
               
                delegate.deliverMessage(message);

        }
}
